import 'package:flutter/material.dart';

import '../../core/elegance_theme.dart';

/// 进度条类型枚举
enum EProgressType {
  line,
  circle,
  dashboard
}

/// 进度条状态枚举
enum EProgressStatus {
  success,
  exception,
  warning,
  info
}

/// 优雅UI进度条组件
class EProgress extends StatefulWidget {
  /// 当前进度值，范围0-100
  final double percentage;
  
  /// 进度条类型
  final EProgressType type;
  
  /// 进度条宽度/直径
  final double width;
  
  /// 进度条高度/线条宽度
  final double strokeWidth;
  
  /// 自定义颜色
  final Color? color;
  
  /// 背景颜色
  final Color? backgroundColor;
  
  /// 是否显示文本
  final bool showText;
  
  /// 文本是否在进度条内部
  final bool textInside;
  
  /// 自定义文本格式化函数
  final String Function(double percentage)? format;
  
  /// 进度条状态
  final EProgressStatus? status;
  
  /// 是否禁用
  final bool disabled;
  
  const EProgress({
    super.key,
    required this.percentage,
    this.type = EProgressType.line,
    this.width = 200,
    this.strokeWidth = 6,
    this.color,
    this.backgroundColor,
    this.showText = true,
    this.textInside = false,
    this.format,
    this.status,
    this.disabled = false,
  });

  @override
  State<EProgress> createState() => _EProgressState();
}

class _EProgressState extends State<EProgress> with ThemedMixin {
  @override
  Widget build(BuildContext context) {
    if (widget.disabled) {
      return _buildDisabledProgress();
    }
    
    switch (widget.type) {
      case EProgressType.circle:
      case EProgressType.dashboard:
        return _buildCircleProgress();
      case EProgressType.line:
      default:
        return _buildLineProgress();
    }
  }
  
  /// 构建禁用状态的进度条
  Widget _buildDisabledProgress() {
    final progressColor = Colors.grey.shade300;
    final backgroundColor = Colors.grey.shade200;
    
    if (widget.type == EProgressType.line) {
      return Container(
        height: widget.strokeWidth,
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(widget.strokeWidth / 2),
        ),
        child: Container(
          height: widget.strokeWidth,
          width: double.infinity,
          decoration: BoxDecoration(
            color: progressColor,
            borderRadius: BorderRadius.circular(widget.strokeWidth / 2),
          ),
        ),
      );
    } else {
      final radius = (widget.width - widget.strokeWidth) / 2;
      final startAngle = widget.type == EProgressType.dashboard ? 0.75 * 2 * 3.1415926 : 0.5 * 2 * 3.1415926;
      final sweepAngle = widget.type == EProgressType.dashboard ? 1.5 * 2 * 3.1415926 : 2 * 3.1415926;
      
      return SizedBox(
        width: widget.width,
        height: widget.width,
        child: Stack(
          alignment: Alignment.center,
          children: [
            // 背景圆环
            SizedBox(
              width: widget.width,
              height: widget.width,
              child: CircularProgressIndicator(
                value: 1.0,
                backgroundColor: backgroundColor,
                valueColor: AlwaysStoppedAnimation<Color>(progressColor),
                strokeWidth: widget.strokeWidth,
                strokeCap: StrokeCap.round,
              ),
            ),
          ],
        ),
      );
    }
  }
  
  /// 获取进度条颜色
  Color _getProgressColor() {
    if (widget.color != null) {
      return widget.color!;
    }
    
    final theme = context.eleganceTheme;
    
    switch (widget.status) {
      case EProgressStatus.success:
        return theme.successColor;
      case EProgressStatus.exception:
        return theme.errorColor;
      case EProgressStatus.warning:
        return theme.warningColor;
      case EProgressStatus.info:
        return theme.infoColor;
      default:
        return theme.primaryColor;
    }
  }
  
  /// 获取背景颜色
  Color _getBackgroundColor() {
    if (widget.backgroundColor != null) {
      return widget.backgroundColor!;
    }
    
    final theme = context.eleganceTheme;
    return theme.surfaceColor;
  }
  
  /// 获取文本颜色
  Color _getTextColor() {
    final theme = context.eleganceTheme;
    return theme.textPrimaryColor;
  }
  
  /// 获取格式化的进度文本
  String _getProgressText() {
    if (widget.format != null) {
      return widget.format!(widget.percentage);
    }
    
    return '${widget.percentage.toInt()}%';
  }
  
  /// 构建线形进度条
  Widget _buildLineProgress() {
    final progressColor = _getProgressColor();
    final backgroundColor = _getBackgroundColor();
    final textColor = _getTextColor();
    final text = _getProgressText();
    final percentageValue = widget.percentage.clamp(0.0, 100.0);
    
    if (widget.textInside) {
      return LayoutBuilder(
        builder: (context, constraints) {
          return Container(
            height: widget.strokeWidth,
            decoration: BoxDecoration(
              color: backgroundColor,
              borderRadius: BorderRadius.circular(widget.strokeWidth / 2),
            ),
            child: Stack(
              children: [
                Container(
                  height: widget.strokeWidth,
                  width: constraints.maxWidth * (percentageValue / 100),
                  decoration: BoxDecoration(
                    color: progressColor,
                    borderRadius: BorderRadius.circular(widget.strokeWidth / 2),
                  ),
                  alignment: Alignment.center,
                  child: Text(
                    text,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: widget.strokeWidth > 20 ? 14 : widget.strokeWidth * 0.7,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      );
    } else {
      return Column(
        crossAxisAlignment: widget.type == EProgressType.line ? CrossAxisAlignment.start : CrossAxisAlignment.center,
        children: [
          LayoutBuilder(
            builder: (context, constraints) {
              return Container(
                height: widget.strokeWidth,
                decoration: BoxDecoration(
                  color: backgroundColor,
                  borderRadius: BorderRadius.circular(widget.strokeWidth / 2),
                ),
                child: Stack(
                  children: [
                    Container(
                      height: widget.strokeWidth,
                      width: constraints.maxWidth * (percentageValue / 100),
                      decoration: BoxDecoration(
                        color: progressColor,
                        borderRadius: BorderRadius.circular(widget.strokeWidth / 2),
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
          if (widget.showText && !widget.textInside) ...[
            SizedBox(height: 8),
            Text(
              text,
              style: TextStyle(
                color: textColor,
                fontSize: 12,
              ),
            ),
          ],
        ],
      );
    }
  }
  
  /// 构建环形进度条
  Widget _buildCircleProgress() {
    final progressColor = _getProgressColor();
    final backgroundColor = _getBackgroundColor();
    final textColor = _getTextColor();
    final text = _getProgressText();
    final percentageValue = widget.percentage.clamp(0.0, 100.0);
    
    final radius = (widget.width - widget.strokeWidth) / 2;
    final startAngle = widget.type == EProgressType.dashboard ? 0.75 * 2 * 3.1415926 : 0.5 * 2 * 3.1415926;
    final sweepAngle = widget.type == EProgressType.dashboard ? 1.5 * 2 * 3.1415926 * (percentageValue / 100) : 2 * 3.1415926 * (percentageValue / 100);
    
    return SizedBox(
      width: widget.width,
      height: widget.width,
      child: Stack(
        alignment: Alignment.center,
        children: [
          // 背景圆环
          SizedBox(
            width: widget.width,
            height: widget.width,
            child: CircularProgressIndicator(
              value: widget.type == EProgressType.dashboard ? 0.75 : 1.0,
              backgroundColor: backgroundColor,
              valueColor: AlwaysStoppedAnimation<Color>(backgroundColor),
              strokeWidth: widget.strokeWidth,
              strokeCap: StrokeCap.round,
            ),
          ),
          // 进度圆环
          SizedBox(
            width: widget.width,
            height: widget.width,
            child: CircularProgressIndicator(
              value: widget.type == EProgressType.dashboard ? 0.75 * (percentageValue / 100) : percentageValue / 100,
              backgroundColor: Colors.transparent,
              valueColor: AlwaysStoppedAnimation<Color>(progressColor),
              strokeWidth: widget.strokeWidth,
              strokeCap: StrokeCap.round,
            ),
          ),
          // 中心文本
          if (widget.showText) 
            Text(
              text,
              style: TextStyle(
                color: textColor,
                fontSize: widget.width * 0.2,
                fontWeight: FontWeight.bold,
              ),
            ),
        ],
      ),
    );
  }
}